import * as CONSTS from './index.const';
import { IndexService } from './index.service';
import ElEditable from '@components/el-editable';
import { SupplierAndContractComponent } from '~/components/purchase-order/supplier-and-contract';
import { CarrywayAndAddressComponent } from '~/components/purchase-order/carryway-and-address';
import { PurchaseProductsListComponent } from '~/components/purchase-order/purchase-products-list';
let indexService = new IndexService();
export default {
  components: {
    ElEditable,
    SupplierAndContractComponent,
    CarrywayAndAddressComponent,
    PurchaseProductsListComponent
  },
  inject: ['apiMMallWeb'],
  data() {
    return {
      ...CONSTS,
      active_index: 1,
      bus_model: '2',
      is_edit: false,
      extend_model: [],
      form_data: {
        id: '',
        detail_id: '',
        supplier: '',
        contract: '',
        carryway: '',
        address: '',
        address_full: {},
        operation: '',
        remark: '',
        extend: {},
        products: []
      },
      submit_data: {
        page: 1,
        purchaseProduct: []
      },
      readonly_formdata: {
        supplier: '',
        contract: '',
        vendor: '',
        product_line: ''
      }
    };
  },
  beforeRouteUpdate(to, from, next) {
    const page = Number(to.query.page);
    this.active_index = page > 0 ? page : 1;
    next();
  },
  async created() {
    //编辑模式
    if (this.$route.query.id !== undefined && this.$route.query.detailid !== undefined) {
      const route_query_id = this.$route.query.id;
      const route_query_detailid = this.$route.query.detailid;
      this.is_edit = true;
      this.submit_data = await this.apiMMallWeb.GetTSOrderV1ApiPOST({
        submitOrderQuery: { orderId: route_query_id, orderDetailId: route_query_detailid, busModel: this.bus_model }
      });
      let mapping_detail = indexService.mappingOrderDetail(this.submit_data);
      this.form_data = Object.assign({}, this.form_data, mapping_detail);

      //qiyu 2020-3-30 el-form-renderer这个组件form属性watch时做了判断，导致通过数据联动过去的变化无法到内部生效，必须重新赋值
      this.readonly_formdata = indexService.mappingOrderDetailReadonly(this.submit_data);
      const page = this.submit_data.page;
      if (page) {
        this.active_index = page;
      }
    }
    this.setPageData();
  },
  methods: {
    async onSubmit() {
      let verify_result = indexService.verifyStep(3, this.form_data);
      if (verify_result.pass === true) {
        this.submit_data = indexService.mappingSubmitData(this.submit_data, this.form_data);
        let order = await this.apiMMallWeb.TSOrderV1ApiPOST({ ts: this.submit_data });
        let result = await this.apiMMallWeb.OrderSubmitOrderV1ApiPOST({
          submitOrderQuery: { busModel: this.bus_model, orderDetailId: order.orderDetailId, orderId: order.orderId }
        });
        if (result) {
          this.$message({
            message: '订单提交成功',
            type: 'success',
            onClose: () => {
              this.$router.push('list');
            }
          });
        }
      } else {
        this.$notify({
          title: '必填项',
          message: verify_result.message,
          type: 'warning'
        });
      }
    },
    async onNext() {
      if (this.active_index === 1) {
        // if (indexService.diffContract(this.form_data, this.submit_data) === true) {
        //   this.form_data = indexService.resetOrder(this.form_data);
        // }
      }
      let verify_result = indexService.verifyStep(this.active_index, this.form_data);
      if (verify_result.pass === true) {
        await this.onSaveDraft();
        if (this.active_index++ >= 3) this.active_index = 3;
        this.$router.push({ query: { page: this.active_index } });
        this.setPageData();
      } else {
        this.$notify({
          type: 'warning',
          title: '请注意',
          message: verify_result.message
        });
      }
    },
    onPrev() {
      if (this.active_index-- <= 1) this.active_index = 1;
      this.$router.push({ query: { page: this.active_index } });
    },
    onSupplierAndContractChange(val) {
      this.readonly_formdata = indexService.mappingOrderDetailReadonly(val);
      this.submit_data.sorderContractForm = val;
    },
    onAddressChanged(val) {
      this.form_data.address_full = val;
    },
    async onSaveDraft() {
      this.submit_data.busModel = 2;
      this.submit_data.page = this.active_index;
      let order = await this.apiMMallWeb.TSOrderV1ApiPOST({ ts: this.submit_data });
      this.submit_data.orderId = order.orderId;
      this.submit_data.orderDetailId = order.orderDetailId;
    },
    async setPageData() {
      switch (this.active_index) {
        case 1:
          break;
        case 2:
          // this.form_data.products = products.content || [];
          break;
        case 3:
          let append = await this.apiMMallWeb.AppendRelateOrderV1ApiPOST({
            form: { busModel: this.bus_model, productLineCode: 100 }
          });
          this.extend_model = indexService.mappingAppend2Extend(append);
          break;
        default:
          break;
      }
    }
  }
};
